#region License Revision: 0 Last Revised: 6/18/2008 11:00 AM
/******************************************************************************
Copyright (c) Microsoft Corporation.  All rights reserved.


This file is licensed under the Microsoft Public License (Ms-PL). A copy of the Ms-PL should accompany this file. 
If it does not, you can obtain a copy from: 

http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
******************************************************************************/
#endregion // License
using System;
using System.Windows.Browser;
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;

namespace VIEWS // Virtual Earth Wrapper
{

//    public static class ScriptObjectUtility {

//        private const string HelperScript =
//        @"({
//		createDelegate: function(f) {
//		return function() { f.apply(null, arguments); };
//		},
//		enumerate: function(o) {
//		var members = [];
//		for (var m in o) {
//			members.push(m);
//		}
//		return members;
//		}
//		})";

//        private static ScriptObject s_helperObject;

//        static ScriptObjectUtility() {
//            s_helperObject = (ScriptObject)HtmlPage.Window.Eval(HelperScript);
//        }

//        public static object GetItem(this ScriptObject obj, int index) {
//            return obj.GetProperty(index.ToString());
//        }

//        public static T GetItem<T>(this ScriptObject obj, int index) {
//            return obj.GetProperty<T>(index.ToString());
//        }

//        public static T GetProperty<T>(this ScriptObject obj, string member) {
//            object value = obj.GetProperty(member);
//            if (value == null) {
//                return default(T);
//            }

//            Type desiredPropertyType = typeof(T);
//            ScriptObject objectValue = value as ScriptObject;
//            if (objectValue != null) {
//                return objectValue.ConvertTo<T>();
//            }
//            else {
//                return (T)Convert.ChangeType(value, desiredPropertyType, CultureInfo.InvariantCulture);
//            }
//        }

//        public static ScriptObject ToScriptFunction(this Delegate d) {
//            return (ScriptObject)s_helperObject.Invoke("createDelegate", d);
//        }

//        public static IEnumerable<string> GetMembers(this ScriptObject obj) {
//            ScriptObject members = (ScriptObject)s_helperObject.Invoke("enumerate", obj);
//            int length = members.GetProperty<int>("length");
//            for (int i = 0; i < length; i++) {
//                yield return members.GetItem<string>(i);
//            }
//        }
//    }

//    /// <summary>
//    /// JavaScript object wrapper in C#
//    /// </summary>
//    public class JSObject
//    {
//        /// <summary>
//        /// Default constructor initializes object to null
//        /// </summary>
//        public JSObject()
//        { so = null; }

//        /// <summary>
//        /// Constructor takes ScriptObject that will be wrapped.
//        /// </summary>
//        /// <param name="soInit">ScriptObject coming in from script</param>
//        public JSObject(ScriptObject soInit)
//        {
//            so = soInit;
//        }

//        /// <summary>
//        /// local variable to hold the script object, this is the only data for this class
//        /// </summary>
//        private ScriptObject so;

//        /// <summary>
//        /// Used to get the classname of an object, not used currently, but might be handy in the future. Derived classes provide own implementaion.
//        /// </summary>
//        public virtual string ClassName { get { return null; } }

//        /// <summary>
//        /// GetHashCode() get the hash code for the object, just use the so hash code
//        /// </summary>
//        /// <returns></returns>
//        public override int GetHashCode()
//        {
//            return so.GetHashCode();
//        }

//        /// <summary>
//        /// property to get the ScriptObject value
//        /// </summary>
//        public ScriptObject JScriptObject
//        {
//            get { return so; }
//            set { so = value; }
//        }

//        /// <summary>
//        /// Cast any JSObject back to original ScriptObject 
//        /// </summary>
//        /// <param name="jso">JSObject we are casting back to a ScriptObject</param>
//        /// <returns>original ScriptObject</returns>
//        public static implicit operator ScriptObject(JSObject jso)
//        {
//            if (jso == null)
//                return null;
//            return jso.so; 
//        }
//        /// <summary>
//        /// Calls a method (member function) on the Javascript Object
//        /// </summary>
//        /// <param name="name">name of the method</param>
//        /// <param name="args">variable number of arguments</param>
//        /// <returns>return value (if any)</returns>
//        protected object Invoke(string name, params object[] args)
//        {
//            if (so == null)
//                return null;
//            return so.Invoke(name, args);
//        }
//        /// <summary>
//        /// Gets a property value from a JavaScript Object
//        /// </summary>
//        /// <param name="name">name of the property</param>
//        /// <returns>value of the property</returns>
//        protected object GetProperty(string name)
//        {
//            if (so == null)
//                return null;
//            return so.GetProperty(name);
//        }

//        /// <summary>
//        /// Sets a property on a JavaScript object
//        /// </summary>
//        /// <param name="name">property name</param>
//        /// <param name="value">value</param>
//        protected void SetProperty(string name, object value)
//        {
//            if (so == null)
//                return;
//            so.SetProperty(name, value);
//        }

//        /// <summary>
//        /// Get an item from a JavaScript Array
//        /// </summary>
//        /// <param name="index">aray index</param>
//        /// <returns>array element</returns>
//        protected object GetItem(int index)
//        {
//            if (so == null)
//                return null;
//            return GetProperty(index.ToString());
//        }

//        /// <summary>
//        /// Get an item from a JavaScript Array
//        /// </summary>
//        /// <typeparam name="T">Type of the array elements</typeparam>
//        /// <param name="index">aray index</param>
//        /// <returns>array element</returns>
//        protected T GetItem<T>(int index) 
//        {
//            return GetProperty<T>(index.ToString());
//        }

//        /// <summary>
//        /// Helper function to return the appropriate value based on desired return type
//        /// </summary>
//        /// <typeparam name="T">Type to return</typeparam>
//        /// <param name="value">value passed in</param>
//        /// <returns>value as a type 'T'</returns>
//        private T ConvertValue<T>(object value)
//        {
//            if (value == null) {
//                return default(T);
//            }

//            ScriptObject objectValue = value as ScriptObject;
//            if (objectValue != null) {
//                return objectValue.ConvertTo<T>();
//            }
//            else {
//                Type desiredPropertyType = typeof(T);
//                return (T)Convert.ChangeType(value, desiredPropertyType, CultureInfo.InvariantCulture);
//            }
//        }
		
//        /// <summary>
//        /// Gets a property value from a JavaScript Object, returns default value of type T if null
//        /// </summary>
//        /// <typeparam name="T">Type of the property</typeparam>
//        /// <param name="name">name of the property</param>
//        /// <returns>value of the property</returns>
//        protected T GetProperty<T>(string member)
//        {
//            object value = GetProperty(member);
//            return ConvertValue<T>(value);
//        }

//        /// <summary>
//        /// Calls a method (member function) on the Javascript Object, Templated return value version
//        /// </summary>
//        /// <typeparam name="T">Type to use for a return value</typeparam>
//        /// <param name="name">name of the method</param>
//        /// <param name="args">variable number of arguments</param>
//        /// <returns>return value of type 'T', returns default(T) for null </returns>
//        protected T Invoke<T>(string name, params object[] args)
//        {
//            object value = Invoke(name, args);
//            return ConvertValue<T>(value);
//        }

//        /// <summary>
//        /// Helper function to C# wrapper arrays to JavaScript ScriptObject arrays
//        /// </summary>
//        /// <param name="arrJSO">array of JSObjects</param>
//        /// <returns>JavaScrip array containing the ScriptObject handles</returns>
//        static protected JSArray<ScriptObject> ConvertToScriptObjectJSArray(JSObject[] arrJSO)
//        {
//            if (arrJSO == null)
//                return null;
//            int ilim = arrJSO.GetLength(0);
//            JSArray<ScriptObject> arrSo = new JSArray<ScriptObject>(ilim);

//            for (int i = 0; i < ilim; ++i)
//            {
//                arrSo[i] = (ScriptObject)arrJSO[i];
//            }
//            return arrSo;
//        }

//        static protected T[] ConvertToArray<T>(JSArray<ScriptObject> arrJS) where T : JSObject, new()
//        {
//            if (arrJS == null)
//                return null;
//            T[] arrT = new T[arrJS.Length];
//            for (int i = 0; i < arrT.Length; ++i)
//            {
//                arrT[i] = new T();
//                arrT[i].JScriptObject = arrJS[i];
//            }
//            return arrT;
//        }

//    }

//    public class JSArray<T> : JSObject
//    {
//        /// <summary>String holding JavaScript class name</summary>
//        private static string thisClass = "Array";
		
//        /// <summary>Get the JavaScript class name</summary>
//        /// <returns type="String">The class name used in JavaScript </returns>		
//        public override string ClassName
//        {
//            get { return thisClass; }
//        }

//        /// <summary>create a new instance of a class and return it</summary>
//        /// <param name="parameters">A variable list of parameters.</param>
//        /// <returns type="ScriptObject">The ScriptObject created </returns>		
//        static public ScriptObject CreateClass(params object[] parameters)
//        {
//            return HtmlPage.Window.CreateInstance(thisClass, parameters);
//        }

//        /// <summary>Construct a C# wrapper from a ScriptObject</summary>
//        /// <param name="so">A ScriptObjet from JScript.</param>
//        public JSArray(ScriptObject so) : base(so) { }

//        /// <summary>
//        /// Construct a JavaScript Array with an inital length set
//        /// </summary>
//        /// <param name="length">initial length of the array</param>
//        public JSArray(int length)
//        {
//            JScriptObject = CreateClass();
//            SetProperty("length", length);
//        }

//        /// <summary>
//        /// Construct an empty JavaScript Array
//        /// </summary>
//        public JSArray()
//        {
//            JScriptObject = CreateClass();
//            SetProperty("length", 0);
//        }

//        /// <summary>
//        /// Length of the array (number of elements)
//        /// </summary>
//        public int Length
//        {
//            get{return GetProperty<int>("length");}
//        }
		
//        /// <summary>
//        /// Indexer that allows us to access like a normal array
//        /// </summary>
//        /// <param name="index">index to the element we want</param>
//        /// <returns>an object of the appropriate type</returns>
//        public T this [int index]	// Indexer declaration
//        {
//            get 
//            {
//                // Check the index limits.
//                if (index < 0 || index >= Length)
//                    return default(T);
//                else // Javascript arrays are just objects with numbered properties
//                    return GetProperty<T>(index.ToString());
//            }
//            set 
//            {
//                if (index < 0)
//                    return;
//                if (index >= Length)
//                {
//                    SetProperty("length", index);
//                }
//                // Javascript arrays are just objects with numbered properties
//                SetProperty(index.ToString(), value);
//            }
//        }

    //}
	
}
